﻿using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Admin.DAO
{
    public class SubmitDAO : BaseDAO, ISubmitDAO
    {
        /// <summary>
        /// 保存提交设置信息
        /// </summary>
        /// <returns></returns>
        public bool InsertOption(SYS_CMS_OPTION obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_OPTION>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除SYS_CMS_OPTION表对应标识位的数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteOption(int flage)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_OPTION> exp = new ExpressionCombine<SYS_CMS_OPTION>();
                exp.And("FLAG", OperatorType.Equal, flage);
                result = Context.Delete<SYS_CMS_OPTION>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 删除_MetaData表对应的数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteMetaData(string dbcode, int sysid)
        {
            bool result = false;
            try
            {
                string sqldelete = "delete from " + dbcode + "_METADATA" + " where SYS_FLD_SYSID=" + sysid;
                result = Context.ExecuteNonQuery(sqldelete);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除_MetaDataFile表对应的数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteMetaDataFile(string dbcode, int sysid)
        {
            bool result = false;
            try
            {
                string sqldelete = "delete from " + dbcode + "_METADATA_FILE " + " where  RECORDID=" + sysid;
                result = Context.ExecuteNonQuery(sqldelete);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除_MetaDataRef表对应的数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteMetaDataRef(string dbcode, int sysid)
        {
            bool result = false;
            try
            {
                string sqldelete = "delete from " + dbcode + "_METADATA_REF " + " where  RECID=" + sysid;
                result = Context.ExecuteNonQuery(sqldelete);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取不合格因素
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public IList<SYS_CMS_ERRORINFOCONFIG> GetErrorInfo()
        {
            IList<SYS_CMS_ERRORINFOCONFIG> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_ERRORINFOCONFIG>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 插入不合格因素
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool InsertErrorInfo(SYS_CMS_ERRORINFOCONFIG obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_ERRORINFOCONFIG>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 移除不合格因素
        /// </summary>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool DeleteErrorInfo(int sysid)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_ERRORINFOCONFIG> exp = new ExpressionCombine<SYS_CMS_ERRORINFOCONFIG>();
                if (sysid != 0)
                {
                    exp.And("SYSID", OperatorType.Equal, sysid);
                }
                result = Context.Delete<SYS_CMS_ERRORINFOCONFIG>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除所有数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteErrorInfoAll()
        {
            bool result = false;
            try
            {
                string sqldelete = "delete from SYS_CMS_ERRORINFOCONFIG";
                result = Context.ExecuteNonQuery(sqldelete);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public bool UpdateMetaData(string dbcode, string markstate, string errorinfo, string sysid)
        {
            bool result = false;
            try
            {
                sysid = sysid.Replace(',', '+');

                string strupdate = "update " + dbcode + "_METADATA set sys_fld_markstate=" + markstate
                    + ",sys_fld_checkdate='" + System.DateTime.Now + "'" + ",sys_fld_checkstate=" + markstate;
                if (SessionHelper.GetUserInfo() != null)
                {
                    strupdate += ",sys_fld_checkusername=" + SessionHelper.GetUserInfo().UserCode;
                }
                if (!String.IsNullOrEmpty(errorinfo))
                {
                    strupdate += ",sys_fld_errorinfo=" + errorinfo;
                }
                else
                {
                    strupdate += ",sys_fld_errorinfo=''";
                }

                strupdate += " WHERE  sys_fld_sysid=(" + sysid + ")";
                result = Context.ExecuteNonQuery(strupdate);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取数字对象使用字段
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_FIELD> GetNumField(string dbcode)
        {
            IList<SYS_CMS_DATABASE_FIELD> list = null;
            try
            {
                string sql = @"select Field from SYS_CMS_DATABASE_FIELD where (DATATYPE=0 or DATATYPE=1 or DATATYPE=2) and DATABASECODE={0}
                                    and NULLENABLE =0 and field =* not field =sys_?";
                list = Context.ExecuteSql<SYS_CMS_DATABASE_FIELD>(string.Format(sql, dbcode));
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return list;
        }
        /// <summary>
        /// 更新SYS_CMS_DATABASE表的 papertype  PaperFieldValue DatetimeFlag 字段
        /// </summary>
        /// <param name="papertype"></param>
        /// <param name="field"></param>
        /// <param name="timecheck"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool UpdateCmsDataBase(string papertype, string field, string timecheck, string sysid)
        {
            bool result = false;
            try
            {
                string strupdate = "update SYS_CMS_DATABASE set PaperType={0},PaperFieldValue='{1}',DatetimeFlag={2} where sysid={3}";
                result = Context.ExecuteNonQuery(String.Format(strupdate, papertype, field, timecheck, sysid));
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取设置过提交样式的字段
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SYS_CMS_SUBMIT> IsEditStyle(string dbcode, string type)
        {
            IList<SYS_CMS_SUBMIT> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_SUBMIT> exp = new ExpressionCombine<SYS_CMS_SUBMIT>();
                exp.And("DbCode", OperatorType.Equal, dbcode);
                exp.And("Type", OperatorType.Equal, type);
                result = Context.Get<SYS_CMS_SUBMIT>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存SYS_CMS_SUBMIT设置信息
        /// </summary>
        /// <returns></returns>
        public bool InsertStype(SYS_CMS_SUBMIT obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_SUBMIT>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除SYS_CMS_SUBMIT表对应数据库的数据
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public bool DeleteStype(string dbcode,string type)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_SUBMIT> exp = new ExpressionCombine<SYS_CMS_SUBMIT>();
                exp.And("dbcode", OperatorType.Equal, dbcode);
                exp.And("type", OperatorType.Equal, type);
                result = Context.Delete<SYS_CMS_SUBMIT>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
